package com.cyf.plc.ui.fragment

import android.annotation.SuppressLint
import androidx.lifecycle.Observer
import androidx.lifecycle.ViewModelProvider
import com.cyf.library.util.logi
import com.cyf.plc.R
import com.cyf.plc.base.BaseFragment
import com.cyf.plc.bean.PanelStatus
import com.cyf.plc.constant.Constants

import com.cyf.plc.model.PlcAttribute
import com.cyf.plc.plc.PlcExecutor
import com.cyf.plc.bean.PlcWriteAttribute

import com.cyf.plc.databinding.FragmentPlcTempBinding
import com.cyf.plc.util.setValueText
import com.cyf.plc.viewmodel.PlcViewModel
import kotlinx.android.synthetic.main.fragment_plc_temp.*
import kotlinx.coroutines.ExperimentalCoroutinesApi
import org.greenrobot.eventbus.Subscribe
import org.greenrobot.eventbus.ThreadMode

@ExperimentalCoroutinesApi
class PlcTempFragment : BaseFragment<PlcViewModel, FragmentPlcTempBinding>() {

    private var panelStatus: PanelStatus = PanelStatus(0f, 0, false, 0f)

    //系统<开关开，开关关>
    private val plcSubAttributeIds = arrayOf(
        //system_power_on system_power_off
        Constants.FLAG_PANEL + Constants.UNDERLINE + Constants.ATTR_POWER + Constants.UNDERLINE + Constants.SUB_ATTR_ON,
        Constants.FLAG_PANEL + Constants.UNDERLINE + Constants.ATTR_POWER + Constants.UNDERLINE + Constants.SUB_ATTR_OFF
    )

    override fun layoutId(): Int = R.layout.fragment_plc_temp

    override fun createViewModel(): PlcViewModel = activity?.run {
        ViewModelProvider(this)[PlcViewModel::class.java]
    } ?: throw Exception("Invalid Activity")

    override fun initView() {
        val panelStatus = arguments?.getParcelable<PanelStatus>(Constants.PANEL_STATUS)
        "获得面板状态:$panelStatus".logi()

        if (panelStatus != null) {
            this.panelStatus = panelStatus

            //读取开关的配置信息
            mViewModel.getPlcSubAttributes(plcSubAttributeIds)
                .observe(viewLifecycleOwner, Observer { it ->
                    "获得属性la".logi()
                    it.forEach {
                        when (it.attributeId) {
                            plcSubAttributeIds[0] -> {
                                panelStatus.powerOnAttribute = it
                            }
                            plcSubAttributeIds[1] -> {
                                panelStatus.powerOffAttribute = it
                            }
                        }
                    }
                    //读取状态
                    PlcExecutor.read(panelStatus.setTempPlcAttribute)
                    PlcExecutor.read(panelStatus.tempPlcAttribute)
                    PlcExecutor.read(panelStatus.powerPlcAttribute)
                    PlcExecutor.read(panelStatus.humidityPlcAttribute)
                })

            //初始化控件点击事件
            iv_temp_minus.setOnClickListener {
                val writeAttribute = PlcWriteAttribute(
                    panelStatus.setTempPlcAttribute.offSet,
                    (--panelStatus.setTemp).toFloat() * 10,
                    panelStatus.powerPlcAttribute.addressUnit
                )
                PlcExecutor.write(writeAttribute)
            }
            iv_temp_plus.setOnClickListener {
                val writeAttribute = PlcWriteAttribute(
                    panelStatus.setTempPlcAttribute.offSet,
                    (++panelStatus.setTemp).toFloat() * 10,
                    panelStatus.setTempPlcAttribute.addressUnit
                )
                PlcExecutor.write(writeAttribute)
            }
            iv_power.setOnClickListener {
                val writeAttribute = PlcWriteAttribute(
                    panelStatus.powerPlcAttribute.offSet,
                    if (panelStatus.power) panelStatus.powerOffAttribute.attributeValue else panelStatus.powerOnAttribute.attributeValue,
                    panelStatus.powerPlcAttribute.addressUnit
                )
                PlcExecutor.write(writeAttribute)
            }
        }
    }

    @SuppressLint("SetTextI18n")
    @Subscribe(threadMode = ThreadMode.MAIN)
    fun getPlcAttributeEvent(plcAttribute: PlcAttribute) {
        ("获取到Attribute更新:$plcAttribute").logi()
        var shouldUpdate = false
        when (plcAttribute.attributeId) {
            panelStatus.powerPlcAttribute.attributeId -> {
                shouldUpdate = true
                panelStatus.power = (plcAttribute.value == panelStatus.powerOnAttribute.attributeValue)
            }
            panelStatus.tempPlcAttribute.attributeId -> {
                shouldUpdate = true
                panelStatus.temp = plcAttribute.value / 10f
            }
            panelStatus.setTempPlcAttribute.attributeId -> {
                shouldUpdate = true
                panelStatus.setTemp = (plcAttribute.value / 10f).toInt()
            }
            panelStatus.humidityPlcAttribute.attributeId -> {
                shouldUpdate = true
                panelStatus.humidity = plcAttribute.value / 10f
            }
        }
        if (shouldUpdate) {
            "更新面板状态:$panelStatus".logi()
            //开关状态
            mViewBinding.panelStatus = panelStatus
            //更新设定温度
            val setTemp = panelStatus.setTemp.toString()
            setValueText(setTemp, tv_set_temp, "℃")
            //更新显示温度
            val temp = panelStatus.temp.toString()
            setValueText(temp, tv_show_temp, "℃")
            //湿度
            val humidity = panelStatus.humidity.toString()
            tv_humidity.text = humidity
        }
    }


}